interface TreeNode {

    val: number;
    left?: TreeNode | null;
    right?: TreeNode | null;
}

const maxIndex = (nums: number[]) => {
    const max = Math.max(...nums)
    return nums.findIndex(n => n === max)
}

var constructMaximumBinaryTree = function (nums: number[]): TreeNode | null {

    if (nums.length === 0) {
        return null
    }

    if (nums.length === 1) {
        return {val: nums[0], left: null, right: null}
    }

    const p = maxIndex(nums)

    return {
        val: nums[p],
        left: constructMaximumBinaryTree(nums.slice(0, p)),
        right: constructMaximumBinaryTree(nums.slice(p + 1))
    }
};

console.log(constructMaximumBinaryTree([3, 2, 1, 6, 0, 5]));